<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Register_model extends CI_Model {

    public function getRegList($limit = 10, $offset = 0) {
        $this->db->where('status > ', 0);
        $query = $this->db->get('user_register', $limit, $offset);
        return $query->result_array();
    }
    
	public function countRegList() {
        $this->db->where('status > ', 0);
        $query = $this->db->get('user_register');
        return $query->num_rows();
    }

    public function addUserRegister($value = array()) {
        $data = array(
            'email' => $value['email'],
            'value' => $value['value'],
            'referral_uid' => $value['referral_uid'],
            'date_created' => date("Y-m-d H:i:s")
        );

        $this->db->insert('user_register', $data);
        
        return $this->db->insert_id();
    }

    public function updateRegUser($id) {
        $data = array(
            'status' => '2',
        );

        $this->db->where('id', $id);
        return $this->db->update('user_register', $data);
    }

    public function getRegUser($id, $status) {
        $this->db->where('id', $id);
        $this->db->where('status', $status);
        
        $query	= $this->db->get('user_register');
        $result = $query->result_array();
        
        if (count($result) > 0)
            return $result[0];
        else
            return false;
    }

    public function updateConfirm($id) {
        $data = array(
            'date_approved' => date("Y-m-d H:i:s"),
            'status' => "1"
        );

        $this->db->where('id', $id);
        return $this->db->update('user_register', $data);
    }

    public function checkMember($uid) {
        $this->db->where('uid', $uid);
        $query = $this->db->get('member');
        $result = $query->result_array();
        if (count($result) > 0)
            return $result[0];
        else
            return false;
    }

    public function getMemberById($uid) {
        $this->db->where('uid', $uid);
        $query = $this->db->get('member');
        $result = $query->result_array();
        if (count($result) > 0)
            return $result[0];
        else
            return false;
    }

    public function activeMember($uid) {
        $data = array(
            'status' => '1',
            'date_modified' => date("Y-m-d H:i:s")
        );

        $this->db->where('uid', $uid);
        return $this->db->update('member', $data);
    }

    public function addUserMember($value = array()) {
        $data = array(
            'uid' => $value['uid'],
            'email' => $value['email'],
            'pin' => $value['pin'],
            'referral_uid' => $value['referral_uid'],
            'date_created' => date("Y-m-d H:i:s"),
        	'status' => '1'
        );
        return $this->db->insert('member', $data);
    }

    public function updateMemberById($value = array()) {
        $data = array(
            'pin' => $value['pin'],
            'gender' => $value['gender'],
            'ktp' => $value['ktp'],
            'name' => $value['name'],
            'birth_date' => $value['birth_date'],
            'birth_place' => $value['birth_place'],
            'address' => $value['address'],
            'postal_code' => $value['postal_code'],
            'province' => $value['province'],
            'mother_name' => $value['mother_name'],
            'phone' => $value['phone'],
            'bank_name' => $value['bank_name'],
            'bank_branch' => $value['bank_branch'],
            'bank_account ' => $value['bank_account'],
            'bank_username' => $value['bank_username'],
            'heir_name' => $value['heir_name'],
            'heir_relation' => $value['heir_relation'],
            'status' => "2",
            'date_modified' => date("Y-m-d H:i:s")
        );
        $this->db->where('uid', $value['uid']);
        return $this->db->update('member', $data);
    }

    public function getUserLogin($value = array()) {
        $data = array(
            'uid' => $value['uid'],
            'pin' => $value['pin']
        );

        $query = $this->db->get_where('member', $data);
        $result = $query->result_array();
        if ($result)
            return $result[0];
        else
            return false;
    }
    
	public function checkMemberByEmail($email) {
        $this->db->where('email', $email);
        $query = $this->db->get('member');
        $result = $query->num_rows();
        
        return $result;
    }

}